import Vue from "vue";

/**
 * 查询详情
 * @param self
 * @param tiyData
 */
export const showEmail = (self, tiyData,index) => {
  tiyData.tData.emailDetail.content='';
  tiyData.tData.emailDetail.attachment={};
  let emailInfo=tiyData.tData.emailList[index];
  let param = {
    "id": emailInfo.id,
    "type": "send",
    "yunKey": emailInfo.yunKey,
  };
  self.$apiEmail.queryEmailDetail(param).then(function (response) {
    if(response.success){
      let emailSend = response.result;
      tiyData.tData.emailDetail.content=emailSend.content;
      tiyData.tData.emailDetail.attachment=emailSend.attachmentList;
      if(emailSend.attachmentList.length>0){
        tiyData.tData.emailDetail.attachmentShow=true;
        tiyData.tData.emailDetail.attachmentCount=`附件 (${emailSend.attachmentList.length}个)`;
      }else{
        tiyData.tData.emailDetail.attachmentShow=false;
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });

  tiyData.tData.emailDetail.id=emailInfo.id;
  tiyData.tData.emailDetail.senderEmail=emailInfo.senderEmail;
  tiyData.tData.emailDetail.virtualName=emailInfo.virtualName;
  tiyData.tData.emailDetail.createTime=emailInfo.createTime;
  tiyData.tData.emailDetail.subject=emailInfo.subject;
  //加载历史邮件列表
  self.$refs.AEmailDetail.emailHistoryCount(emailInfo.virtualName);
  tiyData.tStatus.showEmail.isShowing=true;
}

export const again=(self, tiyData,index)=>{
  let emailInfo=tiyData.tData.emailList[index];
  tiyData.tParam.email.selectedUsers=[];
  let item={
    contactEmail:emailInfo.virtualName,
  };
  tiyData.tParam.email.selectedUsers.push(item);
  self.$refs.sendEmail.email.title='';
  self.$refs.sendEmail.email.content='';
  tiyData.tStatus.showEmail.isShowing=false;
  tiyData.tStatus.replayEmail.isShowing=true;
}

//查询已发送邮件
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$ySpliceAll(tiyData.tData.emailList);

  let param = {
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "subject": tiyData.tParam.subject,
    "virtualName": tiyData.tParam.virtualName,
    "startTime": tiyData.tParam.startTime,
    "endTime": tiyData.tParam.endTime,
    "isAll":true,
    "sysEmail":1,
  };

  if(tiyData.tParam.startTime){
    param.startTime = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParam.startTime).getTime());
  }
  if(tiyData.tParam.endTime){
    param.endTime = Vue.prototype.$tiyQueryTime(new Date(tiyData.tParam.endTime).getTime());
  }

  self.$apiEmail.querySendEmailList(param).then(function (response) {
    if(response.success){
      let emailPage = response.result;
      tiyData.tPage.total = emailPage.total;
      tiyData.tPage.stepNumber = emailPage.pageSize;
      emailPage.list.forEach(
        function (item) {
          tiyData.tData.emailList.push(item);
        }
      );
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
};
